﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BTv2.Controls;
using BTv2.Model;

namespace BTv2.UserControls
{
    /// <summary>
    /// Interaction logic for AdminUsersTab.xaml
    /// </summary>
    public partial class AdminUsersTab : UserControl
    {
        private UsersController _ucontroller;
        private Settings _AdminSettings;
        public AdminUsersTab()
        {
            InitializeComponent();
        }

        public AdminUsersTab(Settings s)
        {
            InitializeComponent();
            _AdminSettings = s;
        }

        private void UsersAdminTabLoaded(object sender, RoutedEventArgs e)
        {
            UserSelectedContent.IsEnabled = false;
            _ucontroller = UsersController.GetInstance();
            UsersListBox.ItemsSource = _ucontroller.Users;
        }

        private void UsersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var user = UsersListBox.SelectedItem as User;
            try
            {
                UsersListContent.IsEnabled = false;
                UserSelectedContent.DataContext = user;
                 UsernameTextbox.IsEnabled = false;
                UserSelectedContent.IsEnabled = true;
            }
            catch
            {
                //when deselecting its called
            }
        }
        private void BackButtonClick(object sender, RoutedEventArgs e)
        {
            UsersListBox.UnselectAll();
            UserSelectedContent.DataContext = null;
            UserSelectedContent.IsEnabled = false;
            UsersListContent.IsEnabled = true;
            AdminToggleSwith.IsChecked = false;
            _AdminSettings.OpenAdminSettings();
        }

        private void CUA_Button_Clicked(object sender, RoutedEventArgs e)
        {
            UsersListBox.UnselectAll();
            UserSelectedContent.DataContext = null;
            UserSelectedContent.IsEnabled = false;
            UsersListContent.IsEnabled = true;
            AdminToggleSwith.IsChecked = false;
        }
        private async void DUA_Button_Clicked(object sender, RoutedEventArgs e)
        {

            User _u = UserSelectedContent.DataContext as User;


            UsersListBox.UnselectAll();
            UserSelectedContent.DataContext = null;
            UserSelectedContent.IsEnabled = false;
            UsersListContent.IsEnabled = true;
            AdminToggleSwith.IsChecked = false;

            bool result = await Task.Run(() => { return _ucontroller.DeleteUser(_u); });
              if (result) _ucontroller.Users.Remove(_u);
        }

        public async void AUA_Button_CLicked(object sender, RoutedEventArgs e)
        {
            UserSettingsMenu.IsOpen = false;
            UsersListContent.IsEnabled = false;
            UserSelectedContent.DataContext = new User();
            UsernameTextbox.IsEnabled = true;
            UserSelectedContent.IsEnabled = true;
        }

        private async void SUA_Button_Clicked(object sender, RoutedEventArgs e)
        {
            User _u = UserSelectedContent.DataContext as User;
            if (_u.ID == 0)
            {
                if (UsernameTextbox.Text == "" || NewpasswordBox.Password == "" || RepeatNewpasswordBox.Password == "" || MailBox.Text == "" || NewpasswordBox.Password != RepeatNewpasswordBox.Password || await _ucontroller.UserExsists(UsernameTextbox.Text))
                {
               if (UsernameTextbox.Text == "") UsernameTextbox.Background = new SolidColorBrush(Colors.Red);
               if (NewpasswordBox.Password == "") NewpasswordBox.Background = new SolidColorBrush(Colors.Red);
               if (RepeatNewpasswordBox.Password == "") RepeatNewpasswordBox.Background = new SolidColorBrush(Colors.Red);
               if (MailBox.Text == "") MailBox.Background = new SolidColorBrush(Colors.Red);
               if (NewpasswordBox.Password != RepeatNewpasswordBox.Password) {
                   NewpasswordBox.Background = new SolidColorBrush(Colors.Red);
                   RepeatNewpasswordBox.Background = new SolidColorBrush(Colors.Red);
               }
               if (await _ucontroller.UserExsists(UsernameTextbox.Text)) UsernameTextbox.Background = new SolidColorBrush(Colors.Red);
               await Task.Delay(4000);
               UsernameTextbox.Background = new SolidColorBrush(Colors.White);
               NewpasswordBox.Background = new SolidColorBrush(Colors.White);
               RepeatNewpasswordBox.Background = new SolidColorBrush(Colors.White);
               MailBox.Background = new SolidColorBrush(Colors.White);
                }else{
                    _u.UserName = UsernameTextbox.Text;
                    _u.Password = NewpasswordBox.Password;
                    _u.Mail = MailBox.Text;
                    _u.IsAdmin =(bool) AdminToggleSwith.IsChecked;

                    bool result = await Task.Run(() => { return _ucontroller.AddUser(_u); });
                    if (result) _ucontroller.Users.Add(_u);

                    NewpasswordBox.Password = "";
                    RepeatNewpasswordBox.Password = "";

                    UsersListBox.UnselectAll();
                    UserSelectedContent.DataContext = null;
                    UserSelectedContent.IsEnabled = false;
                    UsersListContent.IsEnabled = true;
                    AdminToggleSwith.IsChecked = false;


                }
            }
            else
            {
                if (MailBox.Text == "" || NewpasswordBox.Password != RepeatNewpasswordBox.Password )
                {
                   
                    if (MailBox.Text == "") MailBox.Background = new SolidColorBrush(Colors.Red);
                    if (NewpasswordBox.Password != RepeatNewpasswordBox.Password)
                    {
                        NewpasswordBox.Background = new SolidColorBrush(Colors.Red);
                        RepeatNewpasswordBox.Background = new SolidColorBrush(Colors.Red);
                    }             
                    await Task.Delay(4000);
                    NewpasswordBox.Background = new SolidColorBrush(Colors.White);
                    RepeatNewpasswordBox.Background = new SolidColorBrush(Colors.White);
                    MailBox.Background = new SolidColorBrush(Colors.White);
                }
                else
                {
                    if (NewpasswordBox.Password != "") _u.Password = NewpasswordBox.Password;                   
                    _u.Mail = MailBox.Text;
                    _u.IsAdmin = (bool)AdminToggleSwith.IsChecked;

                    bool result = await Task.Run(() => { return _ucontroller.SaveUser(_u); });
                  // if (result) _ucontroller.Users.Add(_u);

                    NewpasswordBox.Password = "";
                    RepeatNewpasswordBox.Password = "";

                    UsersListBox.UnselectAll();
                    UserSelectedContent.DataContext = null;
                    UserSelectedContent.IsEnabled = false;
                    UsersListContent.IsEnabled = true;
                    AdminToggleSwith.IsChecked = false;
                }
            }
        }

        private void OpenUsersSettingsMenu(object sender, MouseButtonEventArgs e)
        {
            UserSettingsMenu.IsOpen = !UserSettingsMenu.IsOpen;
        }
    }
}
